/**
 * API 响应状态码
 */
export enum StatusCode {
  SUCCESS = 200,
  CREATED = 201,
  BAD_REQUEST = 400,
  UNAUTHORIZED = 401,
  FORBIDDEN = 403,
  NOT_FOUND = 404,
  CONFLICT = 409,
  UNPROCESSABLE_ENTITY = 422,
  INTERNAL_SERVER_ERROR = 500,
}

/**
 * API 响应接口
 */
export interface ApiResponse<T = unknown> {
  success: boolean;
  code: number;
  message: string;
  data?: T;
  timestamp: string;
  path?: string;
}

/**
 * 分页响应接口
 */
export interface PaginatedResponse<T = unknown> extends ApiResponse<T[]> {
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
  };
}

/**
 * 创建成功响应
 */
export const createSuccessResponse = <T>(
  data?: T,
  message: string = '操作成功',
  code: number = StatusCode.SUCCESS
): ApiResponse<T> => {
  return {
    success: true,
    code,
    message,
    data,
    timestamp: new Date().toISOString(),
  };
};

/**
 * 创建错误响应
 */
export const createErrorResponse = (
  message: string = '操作失败',
  code: number = StatusCode.INTERNAL_SERVER_ERROR,
  path?: string
): ApiResponse => {
  return {
    success: false,
    code,
    message,
    timestamp: new Date().toISOString(),
    path,
  };
};

/**
 * 创建分页响应
 */
export const createPaginatedResponse = <T>(
  data: T[],
  page: number,
  limit: number,
  total: number,
  message: string = '获取数据成功'
): PaginatedResponse<T> => {
  const totalPages = Math.ceil(total / limit);
  
  return {
    success: true,
    code: StatusCode.SUCCESS,
    message,
    data,
    pagination: {
      page,
      limit,
      total,
      totalPages,
    },
    timestamp: new Date().toISOString(),
  };
};

/**
 * 响应工具类
 */
export class ResponseUtil {
  /**
   * 成功响应
   */
  static success<T>(data?: T, message?: string, code?: number): ApiResponse<T> {
    return createSuccessResponse(data, message, code);
  }

  /**
   * 创建响应
   */
  static created<T>(data?: T, message: string = '创建成功'): ApiResponse<T> {
    return createSuccessResponse(data, message, StatusCode.CREATED);
  }

  /**
   * 错误响应
   */
  static error(message?: string, code?: number, path?: string): ApiResponse {
    return createErrorResponse(message, code, path);
  }

  /**
   * 请求错误响应
   */
  static badRequest(message: string = '请求参数错误'): ApiResponse {
    return createErrorResponse(message, StatusCode.BAD_REQUEST);
  }

  /**
   * 未授权响应
   */
  static unauthorized(message: string = '未授权访问'): ApiResponse {
    return createErrorResponse(message, StatusCode.UNAUTHORIZED);
  }

  /**
   * 禁止访问响应
   */
  static forbidden(message: string = '禁止访问'): ApiResponse {
    return createErrorResponse(message, StatusCode.FORBIDDEN);
  }

  /**
   * 资源未找到响应
   */
  static notFound(message: string = '资源未找到'): ApiResponse {
    return createErrorResponse(message, StatusCode.NOT_FOUND);
  }

  /**
   * 冲突响应
   */
  static conflict(message: string = '资源冲突'): ApiResponse {
    return createErrorResponse(message, StatusCode.CONFLICT);
  }

  /**
   * 服务器内部错误响应
   */
  static internalError(message: string = '服务器内部错误'): ApiResponse {
    return createErrorResponse(message, StatusCode.INTERNAL_SERVER_ERROR);
  }

  /**
   * 分页响应
   */
  static paginated<T>(
    data: T[],
    page: number,
    limit: number,
    total: number,
    message?: string
  ): PaginatedResponse<T> {
    return createPaginatedResponse(data, page, limit, total, message);
  }
}